package ch.fhnw.qualiscan.export;
import java.io.IOException;
import java.io.InputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

import ch.fhnw.qualiscan.application.modals.Export;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import com.vaadin.terminal.StreamResource.StreamSource;


public class Pdf implements StreamSource {
 
	private static final long serialVersionUID = 1L;
	Export exportIstance;
	
	public Pdf() {}
	
	public InputStream getStream() {
		
		try {
			PipedInputStream in = new PipedInputStream();
			final PipedOutputStream out = new PipedOutputStream(in);

			new Thread(new Runnable() {
				public void run() {
					try {
						writePdf(out);
					} catch (Throwable e) {
						throw new RuntimeException(e);
					}
				}
			}).start();
			return in;
		} catch(Throwable e) {
			
		}
		
		return null;
	}
	
	public void writePdf(PipedOutputStream out) throws DocumentException, IOException {
		
		Document document = new Document();
		PdfWriter.getInstance(document, out);
        
        document.open();
        
        document.add(new Paragraph("Hello Test"));
        
		document.close();
	}

}